<template>
  <div class="ele-body">
    <!-- 搜索表单 -->
    <div class="block-interval">
      <a-card :bordered="false">
        <a-form layout="inline" :model="where" @keydown.enter.prevent="reload">
          <a-row>
            <a-form-item label="轮播图标题:">
              <a-input v-model:value.trim="where.title" placeholder="请输入标题" allow-clear />
            </a-form-item>
            <a-form-item class="ele-text-center">
              <a-space>
                <a-button type="primary" @click="reload">查询</a-button>
                <a-button @click="reset">重置</a-button>
              </a-space>
            </a-form-item>
          </a-row>

        </a-form>
      </a-card>
    </div>

    <!-- 表格 -->
    <div>
      <a-card :bordered="false">
        <ele-pro-table
          ref="table"
          row-key="id"
          :datasource="url"
          :columns="columns"
          :where="where"
          :scroll="{ x: 'max-content' }"
        >
          <!-- table上边工具栏 -->
          <template #toolbar>
            <a-space>
              <a-button type="primary" @click="openEdit()">
                <template #icon>
                  <plus-outlined />
                </template>
                <span>新建</span>
              </a-button>
              <!--              <a-button type="danger" @click="removeBatch()">
                              <template #icon>
                                <delete-outlined />
                              </template>
                              <span>删除</span>
                            </a-button>-->
            </a-space>
          </template>

          <!-- table列表状态栏 -->
          <!-- 1是激活，2是禁用 -->
          <template #state="{ record }">
            <a-switch :checked="record.statusFlag === 1" @change="checked => editState(checked, record)" />
          </template>

          <!-- table操作栏按钮 -->
          <template #action="{ record }">
            <a-space>
              <a @click="openEdit(record)">修改</a>
              <a-divider type="vertical" />
              <a-popconfirm title="确定要删除此轮播图吗？" @confirm="remove(record)">
                <a class="ele-text-danger">删除</a>
              </a-popconfirm>
            </a-space>
          </template>

          <template #image="{ record }">
            <a-space>
              <a-image
                preview={false}
                :src="record.imageFileUrl"
                style="height: 50px;"
                alt=""
              />
            </a-space>
          </template>
        </ele-pro-table>
      </a-card>
    </div>
  </div>

  <!-- 编辑弹窗 -->
  <banner-edit v-model:visible="showEdit" :data="current" @done="reload" />
</template>

<script>
import { createVNode } from 'vue';
// 删除，感叹号，加号图标
import { ExclamationCircleOutlined, PlusOutlined } from '@ant-design/icons-vue';
import BannerEdit from './banner-edit.vue';
import { BannerApi } from '@/api/BannerApi';

export default {
  name: 'banner',
  components: {
    PlusOutlined,
    BannerEdit
  },
  data() {
    return {
      // 表格数据接口
      url: '/business/api/banner/info/findPage',
      // 表格列配置
      columns: [
        {
          title: '序号',
          key: 'index',
          width: 48,
          customRender: ({ index }) => this.$refs.table.tableIndex + index
        },
        /*        {
                  title: '序号',
                  dataIndex: 'id',
                  width: 160,
                  sorter: true
                },*/
        {
          title: '标题',
          dataIndex: 'title',
          width: 160
        },
        {
          title: '图片',
          dataIndex: 'imageFileUrl',
          width: 160,
          slots: { customRender: 'image' }
        },
        {
          title: '排序',
          dataIndex: 'dataSort',
          sorter: true,
          width: 160
        },
        {
          title: '发布时间',
          dataIndex: 'createTime',
          sorter: true,
          width: 160
        },
        {
          title: '状态',
          dataIndex: 'status',
          sorter: false,
          align: 'center',
          width: 160,
          slots: { customRender: 'state' }
        },
        {
          title: '操作',
          key: 'action',
          width: 180,
          align: 'center',
          fixed: 'right',
          slots: { customRender: 'action' }
        }
      ],
      // 表格搜索条件
      where: {},
      // 表格选中数据
      selection: [],
      // 当前编辑数据
      current: null,
      // 是否显示编辑弹窗
      showEdit: false
    };
  },
  methods: {
    /**
     * 搜索按钮
     */
    reload() {
      this.selection = [];
      this.$refs.table.reload({ page: 1 });
    },

    /**
     * 重置搜索
     */
    reset() {
      this.where = {};
      this.$nextTick(() => {
        this.reload();
      });
    },

    /**
     * 删除单个
     */
    async remove(row) {
      const result = await BannerApi.del({ id: row.id });
      this.$message.success(result.message);
      this.reload();
    },

    /**
     * 批量删除
     */
    removeBatch() {
      if (!this.selection.length) {
        this.$message.error('请至少选择一条数据');
        return;
      }
      this.$confirm({
        title: '提示',
        content: '确定要删除选中的轮播图吗?',
        icon: createVNode(ExclamationCircleOutlined),
        maskClosable: true,
        onOk: async () => {
          let params = this.selection.map(d => d.id);
          const result = await BannerApi.batchDel(params);
          this.$message.success(result.message);
          this.reload();
        }
      });
    },

    /**
     * 打开编辑弹窗
     */
    openEdit(row) {
      this.current = row;
      this.showEdit = true;
    },

    /**
     * 修改状态
     */
    async editState(checked, row) {
      const id = row.id;
      // 职位状态：1-启用，2-禁用
      const statusFlag = checked ? 1 : 2;
      const result = await BannerApi.edit({ id, statusFlag });
      this.$message.success(result.message);
      row.statusFlag = statusFlag;
    }
  }
};
</script>

<style scoped>
/*搜索框与表格之间的间隙*/
.block-interval {
  margin-bottom: 10px;
}

/*重写顶部搜索表单的样式*/
.ant-form-item {
  margin-bottom: 0;
}
/* 表格斑马样式 **/
/deep/.ant-table-tbody tr:nth-child(2n)
{
  background-color:#fafafa;
}
</style>
